import requests


class APIClient:
    def __init__(self, base_url):
        """
        初始化APIClient类
        :param base_url: API的基础URL
        """
        self.base_url = base_url  # 设置基础URL
        self.token = None  # 初始化token为空

    def login(self, username, password):
        """
        登录接口，获取并保存token
        :param username: 用户名
        :param password: 密码
        """
        url = f"{self.base_url}/login"  # 拼接登录URL
        payload = {
            "username": username,
            "password": password
        }  # 设置请求体
        headers = {
            "Content-Type": "application/json;charset=utf-8",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
        }  # 设置请求头

        # 发送POST请求
        response = requests.post(url, json=payload, headers=headers)

        if response.status_code == 200:  # 判断请求是否成功
            response_data = response.json()
            self.token = response_data.get("data", {}).get("accessToken")  # 提取accessToken
            print("Login successful, accessToken:", self.token)  # 打印成功信息
        else:
            print("Login failed:", response.status_code, response.text)  # 打印失败信息

    def get(self, endpoint):
        """
        发送GET请求
        :param endpoint: 接口的路径
        :return: 响应的JSON数据或None
        """
        url = f"{self.base_url}/{endpoint}"  # 拼接请求URL
        headers = {
            "Authorization": f"{self.token}",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
        }  # 设置请求头，包括Authorization

        # 发送GET请求
        response = requests.get(url, headers=headers)

        if response.status_code == 200:  # 判断请求是否成功
            return response.json()  # 返回响应的JSON数据
        else:
            print("GET request failed:", response.status_code, response.text)  # 打印失败信息
            return None

    def post(self, endpoint, data):
        """
        发送POST请求
        :param endpoint: 接口的路径
        :param data: POST请求的数据
        :return: 响应的JSON数据或None
        """
        url = f"{self.base_url}/{endpoint}"  # 拼接请求URL
        headers = {
            "Authorization": f"{self.token}",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
            "Content-Type": "application/json;charset=utf-8",
        }  # 设置请求头，包括Authorization

        # 发送POST请求
        response = requests.post(url, json=data, headers=headers)

        if response.status_code == 200:
            return response.json()
        else:
            print("POST request failed:", response.status_code, response.text)
            return None



if __name__ == "__main__":
    base_url = "http://172.168.3.100:8080/lcmain"
    username = "admin"
    password = "admin123"

    client = APIClient(base_url)  # 初始化APIClient对象
    client.login(username, password)  # 登录并获取token

    if client.token:  # 如果token存在
        # 调用GET接口
        endpoint = "?type=di&sn=1"  # GET接口的路径
        response = client.get(endpoint)  # 发送GET请求
        if response:
            print("GET response:", response)  # 打印GET请求的响应

        # 调用POST接口
        endpoint = "operationLog"  # POST接口的路径
        data = {
            "startTime": "2024-07-01 08:00:00",
            "endTime": "2024-07-01 17:00:00",
            "pageSize": 10,
            "currentPage": 1,
            "operation_type": "null"
        }  # 设置POST请求的数据
        response = client.post(endpoint, data)  # 发送POST请求
        if response:
            print("POST response:", response)  # 打印POST请求的响应

